package jrunner;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class PersonStats {

	private ImportPersonData personIn;
	private ArrayList<Person> personList;
	private DefaultListModel listModel;
	
	private JFrame frame;
	private JPanel east, south, southNorth, southSouth, west;
	private JPanel namePanel, agePanel, heightPanel, weightPanel, sexPanel;
	private JButton ok, chancel, editPerson, createPerson, erasePerson;
	private JLabel nameLabel, ageLabel, heightLabel, weightLabel, sexLabel;
	private JLabel nameSetting, ageSetting, heightSetting, weightSetting, sexSetting;
	private JTextField nameTextField, ageTextField, heightTextField, weightTextField;
	private JComboBox sexComboBox;
	
	private JList swingPersonList;
	
	
	//Constructors
	private PersonStats(ImportPersonData i){
		this.personIn = i;
		personList = importPerson();
	}
	//Factory Method
	public PersonStats getPersonStatsInstance(ImportPersonData ipd){
		return new PersonStats(ipd);
		
	}
	
	private ArrayList<Person> importPerson(){
		return personIn.importPersonData();
	}
	
	public void createPersonStatsGUI(){
		
		frame = new JFrame();
		frame.setSize(new Dimension(450, 300));
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		west = new JPanel(); 
		east = new JPanel(); 
		south = new JPanel();
		
		east.setLayout(new BoxLayout(east, BoxLayout.Y_AXIS));
		south.setLayout(new BoxLayout(south, BoxLayout.Y_AXIS));
		
		mkWestPanel();
		
		mkEastPanel();
		
		mkSouthPanel();
		
		frame.getContentPane().add(BorderLayout.WEST, west);
		frame.getContentPane().add(BorderLayout.EAST, east);
		frame.getContentPane().add(BorderLayout.SOUTH, south);
		
	}
	private void mkWestPanel(){
		
		
		
		listModel = new DefaultListModel();
		
		for(Person p:personList){
			listModel.addElement(p);
		}
		
		swingPersonList = new JList( listModel );
		
		swingPersonList.addMouseListener( new MouseListListener() );
		
		swingPersonList.setPreferredSize( new Dimension (200,400) );
		
		JScrollPane scrollPane = new JScrollPane(swingPersonList);
		
		west.add(scrollPane);

	}
	private void mkEastPanel(){
		namePanel = new JPanel();
		agePanel = new JPanel();
		heightPanel = new JPanel();
		weightPanel = new JPanel();
		sexPanel = new JPanel();
		
		nameLabel = new JLabel("Name: ");
		nameSetting = new JLabel();
		namePanel.add(nameLabel);
		namePanel.add(nameSetting);
		
		ageLabel = new JLabel("Age: ");
		ageSetting = new JLabel();
		agePanel.add(ageLabel);
		agePanel.add(ageSetting);
		
		heightLabel = new JLabel("Height: ");
		heightSetting = new JLabel();
		heightPanel.add(heightLabel);
		heightPanel.add(heightSetting);
		
		weightLabel = new JLabel("Weight: ");
		weightSetting = new JLabel();
		weightPanel.add(weightLabel);
		weightPanel.add(weightSetting);
		
		sexLabel = new JLabel("Sex: ");
		sexSetting = new JLabel();
		sexPanel.add(sexLabel);
		sexPanel.add(sexSetting);
		
		east.add(namePanel);
		east.add(agePanel);
		east.add(heightPanel);
		east.add(weightPanel);
		east.add(sexPanel);
	}
	private void mkSouthPanel(){
		
		southNorth = new JPanel();
		southSouth = new JPanel();
		
		ok = new JButton("OK");
		chancel = new JButton("Chancel");
		createPerson = new JButton("Create Person");
		editPerson = new JButton("Edit Person");
		erasePerson = new JButton("Erase Person");

		southNorth.add(createPerson);
		southNorth.add(editPerson);
		southNorth.add(erasePerson);

		southSouth.add(ok);
		southSouth.add(chancel);
		
		south.add(southNorth);
		south.add(southSouth);

	}

//	For UNIT Testing
	public static void main(String[] args){
		
		PersonStats ps = new PersonStats(new ImportPersonObject());
		ps.createPersonStatsGUI();
		
	}
	private class MouseListListener implements MouseListener
	{

		@Override
		public void mouseClicked(MouseEvent me) {
			JList l = (JList) me.getSource();
			
			System.out.println
			(
				"Listenelement angeklickt! Listenindex: "
				+l.getSelectedIndex()
			);
			
			int listIndex = l.getSelectedIndex();
			DefaultListModel dlm = (DefaultListModel) l.getModel();
			Person p = (Person) dlm.getElementAt(listIndex);
			
			System.out.println(p);
			
			nameSetting.setText( p.getName() );
			ageSetting.setText( new Integer ( p.getAge() ).toString() );
			heightSetting.setText( new Integer ( p.getHeight() ).toString() );
			weightSetting.setText( new Double ( p.getWeight() ).toString() );
			
			String sex = "";
			if(p.getFemale()) sex = "Female";
			if(p.getMale()) sex = "Male";
			
			sexSetting.setText(sex);
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}

	
	
}
